package top.codecrab.time;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author codecrab
 * @since 2021年06月26日 11:10
 */
public class Times {

    private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");

    public interface Task<T> {
        T execute();
    }

    public static <T> T test(String title, Task<T> task) {
        if (task == null) return null;
        title = (title == null || title.isEmpty()) ? "" : "【" + title + "】";
        System.out.println(title);
        System.out.println("开始：" + FMT.format(LocalTime.now()));
        long begin = System.currentTimeMillis();
        T execute = task.execute();
        long end = System.currentTimeMillis();
        System.out.println("结束：" + FMT.format(LocalTime.now()));
        double delta = (end - begin) / 1000.0;
        System.out.println("耗时：" + delta + "秒");
        System.out.println("-------------------------------------");
        return execute;
    }

}
